﻿Imports PhDir.Repository
Imports PhDirForm.Util
Imports PhDir.Model
Imports System.ComponentModel

Public Class Search

    Private Const DataGridViewEmpty = 1

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or
                     ControlStyles.UserPaint Or
                     ControlStyles.AllPaintingInWmPaint, True)

        Me.DepartmentRepository = PhDir.Main.Instance.DepartmentRepository
        Me.UserRepository = PhDir.Main.Instance.UserRepository
        Me.PhoneRepository = PhDir.Main.Instance.PhoneRepository

        Me.SearchByComboBox.SelectedIndex = 0
    End Sub

    Private Property DepartmentRepository As DepartmentRepository
    Private Property UserRepository As UserRepository
    Private Property PhoneRepository As PhoneRepository
    Private Property Departments As List(Of Department)
    Private Property Phones As List(Of Phone)
    Public Property MainForm As PhDirForm.Form

    Private Sub Search_VisibleChanged(sender As System.Object, e As System.EventArgs) Handles MyBase.VisibleChanged
        If Me.Visible = True And Me.PhoneTable.Rows.Count = DataGridViewEmpty Then
            Me.Phones = Me.PhoneRepository.SelectAll()
            Me.PopulatePhoneTable(Phones)
        End If

        Me.SearchTextBox.Text = String.Empty
    End Sub

    Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
        Select Case Me.SearchByComboBox.SelectedIndex
            Case SearchBy.All
                Me.Phones = Me.PhoneRepository.SelectAll()
            Case SearchBy.Name
                Me.Phones = Me.PhoneRepository.SearchByName(Me.SearchTextBox.Text)
            Case SearchBy.Department
                Me.Phones = Me.PhoneRepository.SearchByDepartment(Me.SearchTextBox.Text)
            Case SearchBy.PhoneNumber
                Me.Phones = Me.PhoneRepository.SearchPlain(Me.SearchTextBox.Text)
        End Select

        Me.PopulatePhoneTable(Phones)
    End Sub

    Private Sub PopulatePhoneTable(phones As List(Of Phone))
        Me.PhoneTable.Rows.Clear()
        Dim lastColumn = Me.PhoneTable.Columns.Count - 1

        For Each phone In phones
            Dim lastRow = Me.PhoneTable.Rows.Count
            Dim userId = phone.User.UserId
            Dim userName = phone.User.UserName
            Dim departmentId = phone.User.Department.DepartmentId
            Dim department = phone.User.Department.DepartmentName
            Dim phoneNumber = phone.PhoneNumber
            'Dim phoneNumbers = (From telephone In phones
            '                   Where telephone.User.UserId = userId
            '                   Select telephone.PhoneNumber).ToList()

            Me.PhoneTable.Rows.Add(userId, userName, departmentId,
                                   department, phoneNumber)
            'Me.PhoneTable(lastColumn, lastRow).Value = userId
            'Dim phoneComboBox As DataGridViewComboBoxCell = Me.PhoneTable(lastColumn, lastRow)
            'phoneComboBox.DataSource = phoneNumbers
        Next
    End Sub

    Private Sub EditNameButton_Click(sender As Object, e As EventArgs) Handles EditNameButton.Click
        Dim userId = Me.PhoneTable.SelectedRows.Item(0).Cells(PhoneTableColumn.UserId)
        Dim editNameControl = Me.MainForm.EditName
        Me.MainForm.HeadingLabel.Text = Me.EditNameButton.Text
        Me.Visible = False
        editNameControl.Show(userId.Value)
    End Sub

    Private Sub EditPhoneButton_Click(sender As Object, e As EventArgs) Handles EditPhoneButton.Click
        Dim userId = Me.PhoneTable.SelectedRows.Item(0).Cells(PhoneTableColumn.UserId)
        Dim oldPhone = Me.PhoneTable.SelectedRows.Item(0).Cells(PhoneTableColumn.PhoneNumber)
        Dim editPhoneControl = Me.MainForm.EditPhone
        Me.MainForm.HeadingLabel.Text = Me.EditPhoneButton.Text
        Me.Visible = False
        editPhoneControl.Show(userId.Value, oldPhone.Value)
    End Sub

    Private Sub DeleteUserButton_Click(sender As Object, e As EventArgs) Handles DeleteUserButton.Click
        Dim userName = Me.PhoneTable.SelectedRows.Item(0).Cells(PhoneTableColumn.UserName)
        Dim userId = Me.PhoneTable.SelectedRows.Item(0).Cells(PhoneTableColumn.UserId)
        Dim user = New User With {.UserId = userId.Value, .UserName = userName.Value}
        Dim confirmDelete = MessageBox.Show(
            String.Format("Are you sure you want to delete ""{0}""?", userName.Value),
            "Delete Confirmation",
            MessageBoxButtons.YesNo,
            MessageBoxIcon.Warning)

        If confirmDelete = DialogResult.Yes Then
            Dim isSuccess = Me.UserRepository.Delete(user)
            If isSuccess Then
                MessageBox.Show("Operation Succeed")
                Me.SearchButton_Click(Nothing, EventArgs.Empty)
            Else
                MessageBox.Show("Operation Failed")
            End If
        End If
    End Sub

    Private Sub DeletePhoneButton_Click(sender As System.Object, e As System.EventArgs) Handles DeletePhoneButton.Click
        Dim phoneNumber = Me.PhoneTable.SelectedRows.Item(0).Cells(PhoneTableColumn.PhoneNumber)
        Dim phone = New Phone With {.PhoneNumber = phoneNumber.Value}
        Dim confirmDelete = MessageBox.Show(
            String.Format("Are you sure you want to delete ""{0}""?", phoneNumber.Value),
            "Delete Confirmation",
            MessageBoxButtons.YesNo,
            MessageBoxIcon.Warning)

        If confirmDelete = DialogResult.Yes Then
            Dim isSuccess = Me.PhoneRepository.Delete(phone)
            If isSuccess Then
                MessageBox.Show("Operation Succeed")
                Me.SearchButton_Click(Nothing, EventArgs.Empty)
            Else
                MessageBox.Show("Operation Failed")
            End If
        End If
    End Sub
End Class
